﻿using System;
using System.Collections.Generic;
using System.Data.Objects.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.PageActionManager;

namespace ZBJF.Warranty.BLL
{
    public partial class SystemUserService
    {
        public SystemUser Login(string userAccount, string password, ApplicationType applicationType, out bool flag)
        {
            var pass = CommonMethod.JiaMi(password);
            var list =
                LoadEntities(
                    c => c.BelongApplicationType == (byte)applicationType && c.UserAccount.Equals(userAccount, StringComparison.OrdinalIgnoreCase) &&
                         c.Password.Equals(pass, StringComparison.OrdinalIgnoreCase) && c.IsAvailable);
            var result = list.FirstOrDefault();
            flag = result != null;
            return result;
        }

        public SystemUser Load(long id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }
        public bool IsExist(string userAccount, ApplicationType applicationType, long id = 0)
        {
            var predicate = PredicateExtensions.True<SystemUser>().And(o => o.UserAccount.Equals(userAccount) && o.BelongApplicationType == (byte)applicationType);
            if (id > 0)
                predicate = predicate.And(o => o.ID != id);
            return LoadEntities(predicate).Any();
        }

        public SystemUser Load(ApplicationType applicationType, string userAccount)
        {
            if (string.IsNullOrEmpty(userAccount))
                return null;
            return LoadEntities(o => o.BelongApplicationType == (byte)applicationType && o.UserAccount.Equals(userAccount, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IQueryable<SystemUser> GetSystemUserAll()
        {
            return LoadEntities(o => true);
        }
        public IQueryable<SystemUser> GetSystemUserAllByInsuranceCompanyId(long insuranceCompanyId)
        {
            return LoadEntities(o => o.InsuranceCompanyID == insuranceCompanyId);
        }

        public IQueryable<SystemUser> GetPagingList(SystemUserModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<SystemUser>();
            if (model.BelongApplicationType > 0)
            {
                expressionWhere = expressionWhere.And(o => o.BelongApplicationType == model.BelongApplicationType);
            }
            if (model.InsuranceCompanyID > 0)
            {
                expressionWhere = expressionWhere.And(o => o.InsuranceCompanyID == model.InsuranceCompanyID);
            }
            if (model.RoleID > 0)
            {
                expressionWhere = expressionWhere.And(o => o.RoleToSystemUser.Any(r => r.RoleID == model.RoleID));
            }
            if (!string.IsNullOrEmpty(model.Keyword))
            {
                expressionWhere =
                    expressionWhere.And(o => (o.UserAccount + SqlFunctions.Space(1) +
                             o.UserDisPlayName).Contains(model.Keyword));
            }

            return LoadPagerEntities<SystemUser>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }

    }
}
